<?php

class ControllerDeviceBase extends Controller {
	
	public function index() {
		$this->response->setOutPut("Welcome");
	}
	
	/*根据imei获取动态令牌*/
	public function auth(){
		$json = array(
			'token'=>null, //令牌
			'ret'=>null //返回结果
		);
		if(!empty($this->request->get['device_imei']) && !empty($this->request->get['device_secret'])){
			$this->load->library('encrypt');
			//加载model里的device文件夹下的device,加载方法
			$this->load->model('device/device');
			//使用request.php下的get请求，过滤其他请求
			$device_imei = $this->request->get['device_imei'];
			//同样的方法，过滤device_secret的数据
			$device_secret = $this->request->get['device_secret'];
			//echo $device_secret;
			//实例化$encrypt类，调用maketoken方法生成token
			$encrypt = new Encrypt($device_imei,$device_secret);
			$token = $encrypt->makeToken();
			if($this->model_device_device->isPassDeviceAuth($device_imei,$device_secret,$token)){
				//通过验证，那么开始生成token
				$json['token'] = $token;
				$json['ret'] = $encrypt->dataEncrypt('pass',$token); //验证通过
			}else{
				$json['token'] = $token;
				$json['ret'] = $encrypt->dataEncrypt('unpass',$token); //验证不通过
			}
			
			$this->response->setOutPut(json_encode($json));
		}else{
			$this->response->setOutPut('error');
		}
	}
}
